﻿#include<stdio.h>
#include<math.h>
#include<GL/glut.h> 
#include<time.h>
#include "bodyShape.h"

// the initial rotation of the spider's body around the X & Y axis
void initRotation(pBody arg)
{
	arg->bodyX=90;
	arg->bodyY=90;
}

// initializing a point in 3D to 0,0,0
void initP(pPoint a)
{
	a->x=0;
	a->y=0;
	a->z=0;
}

// sets the color according the values of a point in 3D
void SetColor(Point a)
{
	glColor3f(a.x,a.y,a.z);
}


// draws the Junctura Main Point
void DrawP()
{
	glPointSize(5.0f); // set point‟s size to 5.0
	glBegin(GL_POINTS); // draw the point
	glVertex2f(0,0);
	glEnd();
	glPointSize(1.0f); // set point‟s size back to 1.0
}

// sets the pointers array to the spider's body structure fields so we can save the values in a file anytime
void LoadBodyDegreesPtr(pBody a)
{
    body_degrees_ptr[0]=&a->UpperRightDegree; 
	body_degrees_ptr[1]=&a->UpperRightYDegree; 
	body_degrees_ptr[2]=&a->UpperRightExtensionDegree;
	body_degrees_ptr[12]=&a->UpperRightE2Degree;

	body_degrees_ptr[3]=&a->UpperLeftDegree;
    body_degrees_ptr[4]=&a->UpperLeftYDegree;
	body_degrees_ptr[5]=&a->UpperLeftExtensionDegree;

	body_degrees_ptr[6]=&a->LowerRightDegree;
	body_degrees_ptr[7]=&a->LowerRightYDegree;
	body_degrees_ptr[8]=&a->LowerRightExtensionDegree;
	
	body_degrees_ptr[9]=&a->LowerLeftDegree;
	body_degrees_ptr[10]=&a->LowerLeftYDegree;
	body_degrees_ptr[11]=&a->LowerLeftExtensionDegree;
}


// initialise the spider's body's parameters
void	initBody(pBody a,FILE *b)
{
    int body_array_load[NUM_OF_DEGREES]; // the body array of parameters ( structure fields values ) loaded from the file body.txt

	/* If the file pointer is not exists , it means that we can't load the values from body.txt . File doesn't exist.
	   That's why we are going to create a new file body.txt with initialized parameter values */
	if (!b)  
	{
		if ( BodyFileInit(b)==ERROR ) // if creating the file didn't succeed , we going to exit the application.
		{
             printf("Can't open file Body.txt!");
			 glutDestroyWindow(id);
		}
        b=fopen("Body.txt","r+"); // after the file body.txt is created , we are going to open it in order to load from it the values
	
	}

    BodyFileLoad(b,body_array_load); // loading the file values of the spider's arms and juncturas angle rotation to the array body_array_load
	LoadBodyDegreesPtr(&arg); // sets the pointers array to the spider's body structure fields so we can save the values in a file anytime
	a->direction=1; // sets the direction of the rotation anti-clockwise
	
	initRotation(a); // the initial rotation of the spider's body around the X & Y axis to 90 degrees each

	// Now we are going input the arms and juncturas values from what the array loaded from the file body.txt
	a->UpperRightDegree=body_array_load[0];  
	a->UpperRightYDegree=body_array_load[1]; 
	a->UpperRightExtensionDegree=body_array_load[2];
	a->UpperRightE2Degree=body_array_load[12];

	a->UpperLeftDegree=body_array_load[3];
    a->UpperLeftYDegree=body_array_load[4];
	a->UpperLeftExtensionDegree=body_array_load[5];

	a->LowerRightDegree=body_array_load[6];
	a->LowerRightYDegree=body_array_load[7];
	a->LowerRightExtensionDegree=body_array_load[8];
	
	a->LowerLeftDegree=body_array_load[9];
	a->LowerLeftYDegree=body_array_load[10];
	a->LowerLeftExtensionDegree=body_array_load[11];

	initP(&a->bodyC); //blue     
	a->bodyC.y=0;
	a->bodyC.x=0;
	a->bodyC.z=255;

	initP(&a->JuncturaC); //yellow  
	a->JuncturaC.x=120;
	a->JuncturaC.y=120;

	initP(&a->MainJuncturaC);//blue  
	a->MainJuncturaC.z=255;

	
}


// the function is responsible for drawing the spiders' body , arms and juncturas
void	drawBody(pBody a)
{
	
	//first off we rotate the body itself on the x,y axises
	glRotated(a->bodyX,1,0,0);
	glRotated(a->bodyY,0,1,0);

   /* We now draws the spider eliptic body */
	glPushMatrix();
		glScaled(1,2,0.6);	//make eclipse
		glColor3d(100,0,0);	//set red
		glutSolidSphere(0.997,60,60);	// draw inner sphere
		SetColor(a->bodyC);	//set blue
		glutWireSphere(1,60,60);	//draw upper net
	glPopMatrix();
     
	// Drawing the Spider's head
	glPushMatrix(); 
		glTranslated(0,2.25,0);
		glColor3d(100,0,0);
		glutSolidSphere(0.25,30,30);
	glPopMatrix();
	
	/* NOW WE ARE GOING TO DRAW EACH LIMB */
	
	SetColor(a->bodyC); // sets the color of the arms and juncturas to BLUE !
	
	/* Draws upper right arm and it's 2 extension juncturas */
	
	glPushMatrix();
	    // Sets the translation and the value of the upper right arm parameters rotation in order to draw it
		glTranslated(0.9,0.9,0); //move to edge of sphere 	
		glRotated(a->UpperRightDegree,0,0,1); //rotate the upper right arm itself around Z axis
		glRotated(a->UpperRightYDegree,0,1,0); //rotate the upper right arm itself around Y axis
		
		//draw arm
		glBegin(GL_LINES);
			glVertex3d(0,0,0);
			glVertex3d(1,1,0);
		glEnd();
		
		// Drawing the Point of the Junctura
		SetColor(a->JuncturaC); // setting the color to YELLOW !
		DrawP(); // drawing the MAIN POINT of the junctura
		
		//now draw extension
			glTranslated(1,1,0); //mov to edge of the arm
			glRotated(a->UpperRightExtensionDegree,0,0.71,0.71); //rotate the extension of the arm around Y & Z axis
			
			SetColor(a->bodyC); // setting the color to BLUE !
			
			// Draws Extentsion
			glBegin(GL_LINES);
				glVertex3d(0,0,0);
				glVertex3d(0.5,0.5,0);
			glEnd();
	
			// Drawing the Point of the 2nd Junctura
			SetColor(a->JuncturaC); // set color to YELLOW
			DrawP(); // drawing the MAIN POINT of the 2nd junctura
		
				//draw second extention
				SetColor(a->bodyC);
				glTranslated(0.5,0.5,0); //mov to edge of the arm
				glRotated(a->UpperRightE2Degree,0,1,0); //rotate the extension of the arm
				glBegin(GL_LINES);
					glVertex3d(0,0,0);
					glVertex3d(0.3,0.3,0);
				glEnd();

			SetColor(a->JuncturaC);
			DrawP();

	glPopMatrix();

	/* Draws upper left arm and it's extension junctura */
	glPushMatrix();
		// Sets the translation and the value of the upper left arm parameters rotation in order to draw it
		glTranslated(-0.9,0.9,0); //move to edge of sphere 	
		glRotated(a->UpperLeftDegree,0,0,1); // rotate the arm itself around Z axis
		glRotated(a->UpperLeftYDegree,0,1,0); // rotate the arm itself around Y axis
		
		//draw arm
		SetColor(a->bodyC);
		glBegin(GL_LINES);
			glVertex3d(0,0,0);
			glVertex3d(-1,1,0);
		glEnd();

		
		SetColor(a->JuncturaC);
		DrawP();
		
		//now draw extension
			glTranslated(-1,1,0); //mov to edge of the arm
			glRotated(a->UpperLeftExtensionDegree,0,0.71,0.71); //rotate the extension of the arm
			SetColor(a->bodyC);
			glBegin(GL_LINES);
				glVertex3d(0,0,0);
				glVertex3d(0.5,0.5,0);
			glEnd();
		
		SetColor(a->JuncturaC);
		DrawP();
				

	glPopMatrix();

	/* Draws lower right arm and it's extension junctura */
	glPushMatrix();
		// Sets the translation and the value of the lower right arm parameters rotation in order to draw it
		glTranslated(0.9,-0.9,0); //move to edge of sphere 	
		glRotated(a->LowerRightDegree,0,0,1); //rotate the arm itself around Z axis
		glRotated(a->LowerRightYDegree,0,1,0); //rotate the arm itself around Y axis
		//draw arm
		SetColor(a->bodyC);
		
		glBegin(GL_LINES);
			glVertex3d(0,0,0);
			glVertex3d(1,-1,0);
		glEnd();
		
		SetColor(a->JuncturaC);
		DrawP();
		
		glPushMatrix();
		//now draw extension
			glTranslated(1,-1,0); //mov to edge of the arm
			glRotated(a->LowerRightExtensionDegree,0,0.71,0.71); //rotate the extension of the arm
			
			SetColor(a->bodyC);
			glBegin(GL_LINES);
				glVertex3d(0,0,0);
				glVertex3d(0.5,0.5,0);
			glEnd();
			
			SetColor(a->JuncturaC);
			DrawP();
		
		glPopMatrix();
				

	glPopMatrix();

	/* Draws lower left arm and it's extension junctura */
	glPushMatrix();
		// Sets the translation and the value of the lower left arm parameters rotation in order to draw it
		glTranslated(-0.9,-0.9,0); //move to edge of sphere 	
		glRotated(a->LowerLeftDegree,0,0,1); //rotate the arm itself
		glRotated(a->LowerLeftYDegree,0,1,0);
		//draw arm
		SetColor(a->bodyC);
		glBegin(GL_LINES);
			glVertex3d(0,0,0);
			glVertex3d(-1,-1,0);
		glEnd();
		
		SetColor(a->JuncturaC);
		DrawP();
		
		glPushMatrix();
		//now draw extension
			glTranslated(-1,-1,0); //mov to edge of the arm
			glRotated(a->LowerLeftExtensionDegree,0,0.71,0.71); //rotate the extension of the arm
			
			SetColor(a->bodyC);
			glBegin(GL_LINES);
				glVertex3d(0,0,0);
				glVertex3d(-0.5,-0.5,0);
			glEnd();
			
			SetColor(a->JuncturaC);
			DrawP();
		
		glPopMatrix();
				

	glPopMatrix();

}
;
			glVertex3d(0,0,0);
			glVertex3d(-1,-1,0);
		glEnd();
		
		SetColor(a->JuncturaC);
		DrawP();
		
		glPushMatrix();
		//now draw extension
			glTranslated(-1,-1,0); //mov to edge of the arm
			glRotated(a->LowerLeftExtensionDegree,0,0.71,0.71); //rotate the extension of the arm
			
			SetColor(a->bodyC);
			glBegin(GL_LINES);
				glVertex3d(0,0,0);
				glVertex3d(-0.5,-0.5,0);
			glEnd();
		